package com.kj.xuyuan.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.ScreenUtils;


public class FirstOpenPopWindow extends PopupWindow {

    private Activity mContext;
    private TextView tvConfirm,tvCancel,tvContent;
//    private TopProgressWebView webview;

    public FirstOpenPopWindow(Activity context) {
        super(context);
        this.mContext = context;
        // 初始化页面
        init();
    }

    // 初始化布局
    private void init() {
        View mView = View.inflate(mContext, R.layout.layout_pop_first_open, null);

        tvConfirm = mView.findViewById(R.id.tv_confirm);
        tvCancel = mView.findViewById(R.id.tv_cancel);
        tvContent = mView.findViewById(R.id.tv_content);
//        webview = mView.findViewById(R.id.webview);

//        webview.loadUrl("https://www.miyushejiao.com/appapi/page/detail?id=10");

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.clickAgree();
                }
                dismiss();
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.exit(0);
            }
        });

        setFocusable(true);
        // 导入布局
        this.setContentView(mView);
        setWidth(ScreenUtils.getScreenWidth(mContext) - DisplayUtil.dp2px(mContext,30));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(false);
        final ColorDrawable dw = new ColorDrawable(0x0000000);
        setBackgroundDrawable(dw);

        setAgreementTextClick();
    }

    private void setAgreementTextClick() {
        String content = tvContent.getText().toString();
        SpannableString spannableString = new SpannableString(content);
        MyClickText myClickText = new MyClickText(mContext, 0);
        MyClickText myClickText2 = new MyClickText(mContext, 1);
        //传入一个MyClickText，并且继承ClickableSpan（必须这样写啊）
        spannableString.setSpan(myClickText, content.indexOf("《用户协议》"),
                content.indexOf("《用户协议》") + 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(myClickText2, content.indexOf("《隐私政策》"), content.indexOf("《隐私政策》") + 6
                , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(spannableString);
        tvContent.setHighlightColor(mContext.getResources().getColor(R.color.transparent)); //设置点击后的颜色为透明
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private class MyClickText extends ClickableSpan {
        private Context context;
        private int type = 0;

        public MyClickText(Activity mainActivity, int type) {
            this.context = mainActivity;
            this.type = type;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            //设置文本的颜色
            ds.setColor(context.getResources().getColor(R.color.ucrop_color_blaze_orange));
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            Bundle bundle = new Bundle();
            if (type == 0) {
                //用户协议
//                WebViewActivity.actionStar(mContext,2);
            } else {
//                WebViewActivity.actionStar(mContext,1);
                //隐私协议
            }
        }
    }

    public void setOnClickListener(OnAgreeListener listener){
        this.listener = listener;
    }

    private OnAgreeListener listener;

    public interface OnAgreeListener{
        void clickAgree();
    }
}
